package com.carck.controller;

import com.carck.pojo.Result;
import com.carck.service.ModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;


@Slf4j
@RestController
public class UploadController {
    @Autowired
    private ModelService modelService;
    @PostMapping("/upload_file")
    @CrossOrigin
    public Result upload(String groupName, String modelName, @RequestParam("file") MultipartFile file) throws IOException, InterruptedException {
        log.info("upload "+groupName+modelName);

        //保存到数据库中
        String fileName=groupName+modelName;
        if(modelService.add(groupName,modelName,fileName)==false)
        {
            return Result.success("该模型已经上传，请更换分组或模型名");
        }
        //本地保存（没条件）
        String zipFileName=groupName+modelName;
        String zipFilePath="/home/carbon/upload_zips/";
        file.transferTo(new File(zipFilePath+zipFileName+".zip"));
        //进行点云重建和相机位姿计算
        //mkdir
        String makeUnzipDirCommand = "mkdir -p "+zipFilePath+zipFileName+"/images" ; // 要执行的命令
        log.info(makeUnzipDirCommand );
        Process process1 = Runtime.getRuntime().exec(makeUnzipDirCommand);
        process1.waitFor();
        //unzip
        String UnzipCommand = "unzip -d "+zipFilePath+zipFileName+"/images"+" "+zipFilePath+zipFileName+".zip"; // 要执行的命令
        log.info(UnzipCommand);
        Process process2 = Runtime.getRuntime().exec(UnzipCommand);
        process2.waitFor();
        //reconstruction script
        String scriptCommand = "/home/carbon/anaconda3/bin/python3.7 "+"/home/carbon/IdeaProjects/carck/src/main/resources/python_script/getpointcloud/extract_camera_pose.py "+zipFilePath+zipFileName;
        log.info(scriptCommand);
        Thread thread=getThread(scriptCommand);
        thread.setDaemon(true);
        thread.start();
        //train nerf弃用
//        String TrainNerfCommand = "/home/carbon/anaconda3/bin/python3.7 "+"/home/carbon/IdeaProjects/carck/src/main/resources/python_script/getnerf/trainNerf.py "+zipFilePath+zipFileName;
//        log.info(TrainNerfCommand);
//        Thread thread1=getThread(TrainNerfCommand);
//        thread1.setDaemon(true);
//        thread1.start();

        return Result.success("上传文件成功!");
    }

    private static Thread getThread(String command) {
        Thread thread = new Thread(() -> {
            try {
                // 执行命令
                Process process = Runtime.getRuntime().exec(command);
                process.waitFor();
                // 读取命令输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    log.info(line);
                }
                // 关闭流
                reader.close();
                process.destroy();
            } catch (IOException | InterruptedException e) {
                log.error(e.toString());
            }
        });
        thread.setDaemon(true);
        return thread;
    }

}
